/**
 * @todo
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void general_trapezoidal()
{
    double
    area = 0,
    a = 0,
    b = 0,
    c = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    printf("Please enter the upper border length: ");
    scanf("%lf", &a);

    printf("Please enter the bottom border length: ");
    scanf("%lf", &b);

    printf("Please enter the height: ");
    scanf("%lf", &c);

    area = (a + b) * c / 2;

    printf("The trapezoidal area is: %lf", area);

    system("PAUSE");
    {
        printf("Continue or not? Yes [y], No [n]?\n");
        scanf("%c", &inputed);

        switch (inputed)
        {
        case 'Y':
        case 'y':
            is_continue = 1;
            break;
        case 'N':
        case 'n':
            is_continue = 0;
            break;
        default:
            ; // TODO
        }

        if (is_continue)
        {
            general_trapezoidal();
        }
        else
        {
            op_general_init();
        }
    }
}

void general_triangle()
{
    double
    a = 0,
    h = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    printf("Please enter the border length: ");
    scanf("%lf", &a);

    printf("Please enter the height: ");
    scanf("%lf", &h);

    if (a > 0 && h > 0) {
        printf("The triangle area is: %lf\n", a * h / 2);

        system("PAUSE");
        {
            printf("Continue or not? Yes [y], No [n]?\n");
            scanf("%c", &inputed);

            switch (inputed)
            {
            case 'Y':
            case 'y':
                is_continue = 1;
                break;
            case 'N':
            case 'n':
                is_continue = 0;
                break;
            default:
                ; // TODO
            }

            if (is_continue)
            {
                general_triangle();
            }
            else
            {
                op_general_init();
            }
        }
    } else {
        printf("Error parameter(s) entered.\n");
        system('PAUSE');
        general_triangle();
    }
}

void general_circular()
{
    double
    r = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    printf("Please enter the circular length: ");
    scanf("%lf", &r);

    if (r > 0) {
        printf("The circular area is: %lf\n", r * r * M_PI);

        system("PAUSE");
        {
            printf("Continue or not? Yes [y], No [n]?\n");
            scanf("%c", &inputed);

            switch (inputed)
            {
            case 'Y':
            case 'y':
                is_continue = 1;
                break;
            case 'N':
            case 'n':
                is_continue = 0;
                break;
            default:
                ; // TODO
            }

            if (is_continue)
            {
                general_circular();
            }
            else
            {
                op_general_init();
            }
        }
    } else {
        printf("Error parameter(s) entered.\n");
        system('PAUSE');
        general_circular();
    }
}

void general_rectangular()
{
    double
    a = 0,
    b = 0,
    area = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    printf("Please enter the length of first border: ");
    scanf("%lf", &a);

    printf("Please enter the length of second border: ");
    scanf("%lf", &b);

    if (a > 0 && b > 0) {
        area = a * b;

        printf("The rectangular area is: %lf\n", area);

        system("PAUSE");
        {
            printf("Continue or not? Yes [y], No [n]?\n");
            scanf("%c", &inputed);

            switch (inputed)
            {
            case 'Y':
            case 'y':
                is_continue = 1;
                break;
            case 'N':
            case 'n':
                is_continue = 0;
                break;
            default:
                ; // TODO
            }

            if (is_continue)
            {
                general_rectangular();
            }
            else
            {
                op_general_init();
            }
        }
    } else {
        printf("Error parameter(s) entered.\n");
        system('PAUSE');
        general_rectangular();
    }
}

void general_sector()
{
    double
    s = 0,
    area = 0,
    r = 0,
    d = 0,
    d1 = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    printf("Please enter the length of radius: ");
    scanf("%lf", &r);

    printf("Please enter the degree of central angle: ");
    scanf("%lf", &d);

    d1 = d / 360;
    s = r * r * M_PI;
    area = d1 * s;

    printf("The sector area is: %lf\n", area);

    system("PAUSE");
    {
        printf("Continue or not? Yes [y], No [n]?\n");
        scanf("%c", &inputed);

        switch (inputed)
        {
        case 'Y':
        case 'y':
            is_continue = 1;
            break;
        case 'N':
        case 'n':
            is_continue = 0;
            break;
        default:
            ; // TODO
        }

        if (is_continue)
        {
            general_sector();
        }
        else
        {
            op_general_init();
        }
    }
}

void op_general_init()
{
    unsigned char selected;

    // Draw UI
    ui_cls();
    ui_add_empty_line(1);
    ui_add_border_line_text("General function selection");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("1: Trapezoidal area");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("2: Triangle area");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("3: Circular area");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("4: Rectangular area");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("5: Sector area");
    ui_add_border_row_empty(2);
    ui_add_opt_desc("6: Back to calculator start");
    ui_add_border_row_empty(1);
    ui_add_border_line_single();
    ui_add_empty_line(1);
    printf("Please enter the operation number you want [1 ~ 6]: ");

    scanf("%d", &selected);
    switch (selected)
    {
    case 1:
        general_trapezoidal();
        break;
    case 2:
        general_triangle();
        break;
    case 3:
        general_circular();
        break;
    case 4:
        general_rectangular();
        break;
    case 5:
        general_sector();
        break;
    case 6:
        index_start();
        break;
    default:
        ui_cls();
        printf("What you had selected is %d, which is incorrect.\n", selected);
        system("PAUSE");
        op_general_init();
    }
}
